package com.cn.goodsadmin.controller;

import com.cn.goodsadmin.common.PageQueryResult;
import com.cn.goodsadmin.dao.DestDTO;
import com.cn.goodsadmin.po.*;
import com.cn.goodsadmin.service.AppService;
import com.cn.goodsadmin.service.UserService;
import com.cn.goodsadmin.service.WordsService;
import com.cn.goodsadmin.service.WxService;
import com.cn.goodsadmin.util.ErrorMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@CrossOrigin(origins = "*", maxAge = 3600)
@Controller
public class WordsController {

    @Autowired
    WxService wxService;

    @Autowired
    AppService appService;

    @Autowired
    WordsService wordsService;

    @Autowired
    UserService userService;

    @LoginRequired
    @RequestMapping(value = "/wxsendmsg", method = RequestMethod.GET,params = {"message","touser","wxNumber","appid","msgid"})
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap wxSendMsg(String message,String touser,String wxNumber,int appid,String msgid) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            boolean iserror=true;
            App app=appService.findAppById(appid);
            if(app!=null) {
                String realAppid=app.getcAppid();
                iserror= wxService.wxSendMsg(message,touser,wxNumber,realAppid,msgid);
            }
            if(iserror){
                supplyMap.setEorrInfo(ErrorMsg.STATUS_SERVICE_BUSY, ErrorMsg.MSG_SERVICE_BUSY);
            }
        } catch (Exception e) {
            e.printStackTrace();
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }

    @LoginRequired
    @RequestMapping(value = "/filterwords", method = RequestMethod.GET,params = {"openid","appid","pageindex","pagesize"})
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap filterWords(String openid,int appid,int pageindex,int pagesize) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            PageQueryResult<DestDTO> pageQueryResult=wordsService.filterWordsByOpenid(pageindex,pagesize,openid,appid);
            if(pageQueryResult!=null){
                WordsPageInfo wordsPageInfo=coverPageTowordsPageInfo(pageQueryResult,appid);
                supplyMap.put("wordsPageInfo",wordsPageInfo);
            }
        } catch (Exception e) {
            e.printStackTrace();
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }


    @LoginRequired
    @RequestMapping(value = "/getwords", method = RequestMethod.GET, params = {"pageindex", "pagesize","appid"})
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap getWords(int pageindex, int pagesize,int appid) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            PageQueryResult<DestDTO> pageQueryResult = wordsService.findAllWords(pageindex, pagesize,appid);
            if(pageQueryResult!=null){
                WordsPageInfo wordsPageInfo=coverPageTowordsPageInfo(pageQueryResult,appid);
                supplyMap.put("wordsPageInfo",wordsPageInfo);
            }

        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }


    private WordsPageInfo  coverPageTowordsPageInfo(PageQueryResult<DestDTO> pageQueryResult,int appid)throws Exception{
        List<DestDTO> destDTOS=pageQueryResult.getQueryResult();
        WordsPageInfo wordsPageInfo=new WordsPageInfo();
        wordsPageInfo.pagecount=pageQueryResult.getPageCount();
        wordsPageInfo.pageIndex=pageQueryResult.getPageIndex();
        wordsPageInfo.pagesize=pageQueryResult.getPageSize();
        wordsPageInfo.totalcount=pageQueryResult.getTotalCount();
        List<Words>   words=new ArrayList<Words>();
        wordsPageInfo.words=words;
        long currTime=System.currentTimeMillis()/1000;
        HashMap<String, CallInfo>  callMap=new HashMap<String, CallInfo>();
        for(DestDTO destDTO:destDTOS){
            Words w= destDTO.getWords();
            String fromuser=w.getcFromusername();
            String touser=w.getcTousername();
            CallInfo callInfo=callMap.get(fromuser);
            if(callInfo==null){
                callInfo=new CallInfo();
                //第一次 查询用户信息
                UserInfo userinfo=userService.findUserInfoByOpenid(fromuser,appid);
                if(userinfo!=null){
                    callInfo.userName=userinfo.getcName();
                }
                //第一次 开始计算
                Words lastWords=wordsService.findLastCallBackByOpenid(fromuser,touser);
                String lasttime=lastWords.getcCtime();
                long subtime=currTime-Long.valueOf(lasttime);
                boolean iscallback=false;
                if(subtime<=2*24*3600){
                    iscallback=true;
                }
                callInfo.iscallBack=iscallback;
                callMap.put(fromuser,callInfo);
            }
            w.iscallBack=callInfo.iscallBack;
            w.userName=callInfo.userName;
            List<Words> callbackWords=wordsService.findCallBackWords(w);
            w.callBackList=callbackWords;
            words.add(w);
        }
        return wordsPageInfo;
    }
}
